//
//  ZHYYTextView.m
//  ZHYYView
//
//  Created by MT-SW-005 on 2021/9/22.
//  Copyright © 2021 yuan zhang. All rights reserved.
//

#import "ZHYYTextView.h"

@implementation ZHYYTextView
{
    NSMutableString *buff;
}

///文本是否可以编辑
- (void)setTextEditable:(BOOL)textEditable {
    if (_textEditable == textEditable) return;
    _textEditable = textEditable;
    [self setNeedsDisplay];
}

///文本是否可以选中
- (void)setTextSelectable:(BOOL)textSelectable {
    if (_textSelectable == textSelectable) return;
    _textSelectable = textSelectable;
    [self setNeedsDisplay];
}


- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
     self.theTextView.textColor = [NSColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:0.95];
    [self.theTextView setFont:self.font];
    self.theTextView.editable = self.textEditable;
    self.theTextView.selectable =self.textSelectable;
  
}


-(id)initWithFrame:(NSRect)frameRect{
    self=[super initWithFrame:frameRect];
    if (self) {
        buff =[NSMutableString string];
        self.textEditable = NO;
        self.textSelectable = YES;
        self.font = [NSFont fontWithName:@"PingFang-SC-Regular" size:14.0];
        [self createScrllTextView];
    }
    return self;
}


- (void)setNeedsDisplay {
    if (self.superview) {
        [self setNeedsDisplay:YES];
    }
}

-(void)setMessage:(NSString*)message{
    [self setMessage:message Cover:YES];
}

-(void)setMessage:(NSString*)message Cover:(BOOL)cover{
    if (cover) {
        [buff appendFormat:@"%@\n",message];
        [_theTextView setString:buff];
        [buff replaceCharactersInRange:NSMakeRange(0, buff.length) withString:@""];
    }else {
        [buff appendFormat:@"%@\n",message];
        [_theTextView setString:buff];
    }
}


-(void)clearMessage {
    [_theTextView setString:@""];
    [buff replaceCharactersInRange:NSMakeRange(0, buff.length) withString:@""];
}



-(NSString*)message{
    return _theTextView.string;
}

//=================================================================================

- (void)addMessage:(NSString *)message
{
    if (message) {
        [self appendMessage:message];
    }
}

- (void)appendMessage:(NSString *)message
{
    NSColor *color;
       if ([[message uppercaseString] containsString:@"ERROR"]||[[message uppercaseString] containsString:@"FAIL"]) {
           color = [NSColor redColor];
       }else if ([[message uppercaseString] containsString:@"OK"]||[[message uppercaseString] containsString:@"SUCCESS"]){
           color =[NSColor greenColor];
       }else{
           color =[NSColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:0.95];
       }
    NSString *messageWithNewLine = [message stringByAppendingString:@"\n"];

    // Smart Scrolling
    BOOL scroll = (NSMaxY(self.theTextView.visibleRect) == NSMaxY(self.theTextView.bounds));

    // Append string to textview
    [self.theTextView.textStorage appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@",[self date],messageWithNewLine] attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:self.font}]];

    
    if (scroll) // Scroll to end of the textview contents
        [self.theTextView scrollRangeToVisible: NSMakeRange(self.theTextView.string.length, 0)];
}

/* date */
- (NSString *)date {
    static NSDateFormatter *formatter;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"[yyyy-MM-dd HH:mm:ss.SSS]"];
    }
    return [formatter stringFromDate:[NSDate date]];
}



-(void)createScrllTextView{
    self.theTextView =[[NSTextView alloc]initWithFrame:self.bounds];
    self.theTextView.backgroundColor = [NSColor whiteColor];
    self.theTextView.editable = _textEditable;
    self.theTextView.selectable = _textEditable;
    
    self.scrollView =[[NSScrollView alloc] init];
    self.scrollView.frame = self.bounds;
    [self.scrollView setBorderType:NSNoBorder];
    [self.scrollView setHasVerticalScroller:YES];
    [self.scrollView setHasHorizontalScroller:NO];
    [self.scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

    [self.theTextView setMinSize:NSMakeSize(0.0, self.window.frame.size.height - 30)];
    [self.theTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
    [self.theTextView setVerticallyResizable:YES];
    [self.theTextView setHorizontallyResizable:YES];
    [self.theTextView setAutoresizingMask:NSViewWidthSizable];
    [[self.theTextView textContainer]setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
    [[self.theTextView textContainer]setWidthTracksTextView:YES];
  
 
    [self.scrollView setDocumentView:self.theTextView];
    [self addSubview:self.scrollView];
}
@end
